package cn.xiaopengstack.controller;


import cn.xiaopengstack.service.IMcpService;
import cn.xiaopengstack.service.request.SendCsdnAndWeixinNoteRequest;
import cn.xiaopengstack.service.response.Response;
import cn.xiaopengstack.service.response.SendCsdnAndWeixinNoteResp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author jiangyangang
 */
@RestController
@RequestMapping(("/ai/api/${app.config.api-version}/mcp"))
@Slf4j
public class McpController implements IMcpService {
    @Resource
    private ChatClient deepseekChatClient;

    @PostMapping("/send_csdn_weixin_note")
    @Override
    public Response<SendCsdnAndWeixinNoteResp> doSendCsdnAndWeixinNote(@RequestBody SendCsdnAndWeixinNoteRequest request) {
        String userInput = request.getUserMsg();
        String resp = deepseekChatClient.prompt(userInput)
                .advisors(advisorSpec -> {
                    advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, "1001");
                    advisorSpec.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 200);
                })
                .call()
                .content();
        log.info("\n>>> QUESTION: {}", userInput);
        log.info("\n>>> ASSISTANT: {}", resp);

        userInput = """
                之后进行，微信公众号消息通知，平台：CSDN、主题：为文章标题、描述：为文章简述、跳转地址：从发布文章到CSDN获取 url
                """;
        resp = deepseekChatClient.prompt(userInput)
                .advisors(advisorSpec -> {
                    advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, "1001");
                    advisorSpec.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 200);
                })
                .call()
                .content();

        log.info("\n>>> QUESTION: {}", userInput);
        log.info("\n>>> ASSISTANT: {}", resp);

        return Response.success(SendCsdnAndWeixinNoteResp.builder().result("success").useTools("[mcp-server-csdn,mcp-server-weixin]").build());
    }
}
